package com.wxtl.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 自定义ID生成工具
 * @author hc24
 * @modify by liulin  修改custId方法
 *
 */
public class ID{

	/** 每秒自增序列 */
    private static int serial = 0;

    /** 当前时间字符�?*/
    private static String time = "";

    /** 自增序列位数 */
    private static final int DIGIT = 3;
    
    private ID(){}

    /**
     * 获取�?��新的不重复的ID
     * 
     * @return 15位长整型数字
     */
    public synchronized static long next(){
        String s = "";
        String t = new SimpleDateFormat("yyMMddHHmmss").format(new Date(System.currentTimeMillis()));
        if(!t.equals(time)){
            time = t;
            serial = 0;
        }
        int max = (int) Math.pow(10, DIGIT);
        if(++serial >= max){
        	try{
        		Thread.sleep(1000L);
        	}catch(InterruptedException e){}
        	return next();
        }
        s += serial;
        while(s.length() < DIGIT){
            s = "0" + s;
        }
        return Long.parseLong(time + s);
    }
    
    private static long sequence1=1;
    private static long sequence2=1;
    private static long sequence3=1;
    
//    /**
//     * 根据当前时间产生订单ID,同一秒钟可以产生900个不同的ID
//     * @return  yyyyMMddHHmmss+xxxx
//     */
//    public synchronized static String orderID(){
//    	Date date=new Date();
//    	String idStr = DateUtil.format(date, "yyyyMMddHHmmss")+sequence;
//    	sequence++;
//    	if(sequence>999)
//    		sequence=100;
//    	return idStr;
//    }
    
    /**
     * 产生代理ID
     */
    public static String agentId(){
    	Date date=new Date();
    	String idStr = "A"+DateUtil.format(date, "yyyyMMdd")+NumberUtil.format(sequence3, "000");
    	sequence1++;
    	if(sequence1>999)
    		sequence1=1;
    	return idStr;
    }
    
    /**
     * 产生授权ID
     */
    public static String authId(){
    	Date date=new Date();
    	String idStr = "A"+DateUtil.format(date, "yyyyMMdd");
    	/*String idStr = "A"+DateUtil.format(date, "yyyyMMdd")+NumberUtil.format(sequence1, "00000");
    	sequence1++;
    	if(sequence1>99999)
    		sequence1=1;*/
    	return idStr;
    }
    
    /**
     * 根据省份和城市产生客户ID
     * @return
     */
    public static String custId(String province,String city){
//    	try {
//			province=new String(province.getBytes("ISO-8859-1"),"UTF-8"); 
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    	ChineseSpelling cp= ChineseSpelling.getInstance();
    	String pSpell1=cp.getSelling(province.substring(0,1)).toUpperCase();
    	String pSpell2=cp.getSelling(province.substring(1,2)).toUpperCase();
    	String p=pSpell1.substring(0,1)+pSpell2.substring(0,1);//省份前两个字的字母的缩写
    	
    	String cSpell1=cp.getSelling(city.substring(0,1)).toUpperCase();
    	String cSpell2=cp.getSelling(city.substring(1,2)).toUpperCase();
    	String c=cSpell1.substring(0,1)+cSpell2.substring(0,1);//市前两个字的字母缩写
    	
    	String pc=p+c;
    	
    	return "C"+pc;
    	
    	/*String idStr="C"+pc+NumberUtil.format(sequence2, "00000000");
  
    	sequence1++;
    	if(sequence1>99999999)
    		sequence1=10000000;
    	
    	return idStr;*/
    }
    
    public static void main(String[] args) {
		System.out.println(ID.next());
		
		System.out.println(authId()+NumberUtil.format(50+1, "0000"));
		System.out.println(authId());
		
		System.out.println(custId("四川", "成都"));
		System.out.println(custId("山东", "德州"));
		System.out.println(custId("四川", "成都")+NumberUtil.format(50+1, "00000000"));
		
		try {
			System.out.println("四川".getBytes("UTF-8").length);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}